package com.iplay.service.version.mobile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iplay.dao.version.mobile.MobileAppVersionDAO;
import com.iplay.entity.version.mobile.MobileAppVersionDO;
import com.iplay.entity.version.mobile.MobileOS;

@Service
public class MobileAppVersionServiceImpl implements MobileAppVersionService {

	@Autowired
	private MobileAppVersionDAO dao;

	@Override
	public double getAndroidAppVersion() {
		return getMobileAppVersion(MobileOS.ANDROID);
	}

	@Override
	public double getIOSAppVersion() {
		return getMobileAppVersion(MobileOS.IOS);
	}

	@Override
	public double setAndroidAppVersion(double version) {
		MobileAppVersionDO app  = dao.save(new MobileAppVersionDO(1, MobileOS.ANDROID, version));
		return app.getVersion();
	}

	@Override
	public double setIOSAppVersion(double version) {
		MobileAppVersionDO app = dao.save(new MobileAppVersionDO(2, MobileOS.IOS, version));
		return app.getVersion();
	}

	private double getMobileAppVersion(MobileOS os) {
		MobileAppVersionDO version = dao.findOneByMobileOS(os);
		return version == null ? 0.0 : version.getVersion();
	}

}
